(function () {
    Codestrong.ui.CreateCategoryDownload= function (_args) {


	var aps='';
	if(Ti.Platform.osname== 'android') {
  		 Titanium.include('../globals.js');
  		 aps='../';
    }
    else {
   		  Titanium.include('globals.js');
    }
	/////////////////////EVENT//////////
	var data = []; 
	var response;
	


	var win = Titanium.UI.createWindow({
            id: 'CategoryDownload',
            title: _args.TenMonHoc,
            backgroundColor: '#FFF',
            barColor: '#414444',
            fullscreen: false,
            modal: true,
    });
      
    var egc = new globals.EventGarbageCollection(win);
    egc.addEventListener('back_CategoryDownload', function () {
 		
 		Codestrong.navGroup.close(win, {
                        animated: true
        });
        
	});
    egc.addEventListener('CategoryDownload:CreateCategoryDownloadDetail', function(data) {
    
	  Codestrong.navGroup.open(Codestrong.ui.CreateCategoryDownloadDetail({
			LectureId:data.LectureId,
			TypeID:data.TypeID,
			LectureName:data.LectureName,
			TenMonHoc:_args.TenMonHoc,
			Price:data.Price,
			TeacherImage:data.TeacherImage,
			IsSee:data.IsSee,
		    IsDownload:data.IsDownload,
		    dataLecture:data.dataLecture,
		    dataLectureArray:data.dataLectureArray	,  		
		}), 
		{
		    animated: true,
		    navBarHidden:true
		});	
					
		
    })
    
    
        
	////////////main_header///////////////////////////
	 var databutton_right=[
    	//{Id:1,Image:aps+'images/list_bullets.png',Image_:aps+'images/list_bullets_.png',tag:'Category'},
	    {Id:2,Image:aps+'images/account.png',Image_:aps+'images/account_.png',tag:'Login'},
	    //{Id:3,Image:aps+'images/search.png',Image_:aps+'images/search_.png',tag:'Search'},
	    
	]
	var main_header=require('controls/main_header').main_header({
	 		backgroundImage: aps+'images/panel3.png',
	 		headertext:'Thầy trò Mobile: '+_args.HeaderText,
		    left: 0,
		    top: 0,
		    height:65,
		    width: Globals.platformWidth,
		    touchEnabled: true		,
		    databutton_right:databutton_right,
		   
		    imback:   aps+'images/back.png',
		    imback_:   aps+'images/back_.png',
		    idWinParent:'CategoryDownload',
    })

	win.add(main_header);
	
	
	var ScrollViewMain = Ti.UI.createScrollView({
		top:main_header.top+main_header.height,
		//height:'100%',
		touchEnabled:true,
	});
	
	var lbLoading = Ti.UI.createLabel({
		text:Globals.loading,
		color:'#1e6cc1',
		font:{
			fontSize:Ti.App.Properties.getString('fontsizeLarge'),
			fontWeight:'bold'
		},
	});
	ScrollViewMain.add(lbLoading)
	
	
	
	var PopularData = function(dataMon) {
		if(dataMon.length==0){lbLoading.text=Globals.lbnodata;}
		else {lbLoading.text='';}
		var Items=dataMon;
		var top_t=0
		var IsMod=0
		var lengthData=Items.length
	
		if(Items.length%2!=0)
		{
			IsMod=1;
			lengthData++;
		}
			
		
		
		for(var i = 0; i < lengthData; i++) {
		   if(i==lengthData-1 && IsMod==1)
		   {
		   		 var viewrightfinal = Ti.UI.createView({
						height:113,
						width:Titanium.Platform.displayCaps.platformWidth/2,
						right:0,
						top:top_t,
						backgroundColor:'transparent',
						//backgroundImage:_args.backgroundImageTable,
						borderRadius:0,
						borderColor:'black',
						borderWidth:1,
						touchEnabled:false,
				});
				ScrollViewMain.add(viewrightfinal);
		   }
		   else
		   {
		   		
		   		
		   		 var viewpanel=require('controls/CategoryDownload_table').CategoryDownload_table({
			  			height:113,
						top:top_t,
						text:Items[i].Name,
						image:aps+'images/teacher/'+Items[i].TeacherImage,//aps+'images/video.png',
						backgroundColorSection:'#2981a3',
						id:Items[i].Id,
						Price:Items[i].Price,
						start:aps+'images/4start.png',
						
						LectureId:Items[i].Id,
						TypeID:Items[i].IdLop,
						LectureName:Items[i].Name,
						TeacherImage:Items[i].TeacherImage,
						IsSee:Items[i].IsSee,
						IsDownload:Items[i].IsDownload,
						dataLecture:Items[i],
						dataLectureArray:dataMon	,  		
				    })
						
				 ScrollViewMain.add(viewpanel);
				
						
				 if(i%2==0)
				 {
				 	viewpanel.left=0;
				 }
				 else
				 {
				 	viewpanel.right=0;
				   	top_t=top_t+viewpanel.height;
				 }   
			}
		}
	}
	
	
	
	
	
	win.add(ScrollViewMain);
	
	
	
	
	 /////////////////////////////FUNCTION///////////////////////
  	var dataMon=[]
    var getListLectureDownload = function() {
		var db = require('db');
		var data = [];
		var row = null;
		var Items = db.selectLectureDownload();
		//alert(Items.length)
		for (var i = 0; i < Items.length; i++) {
			dataMon[i] =Items[i];
		}
	
		PopularData(dataMon)
		Codestrong.ui.activityIndicator.hideModal();
	};
	

		
		
		

	/////////////////////////OPEN WINDOWN/////////////////////////
	
	
	 //if (Ti.Network.online) {
			Codestrong.ui.activityIndicator.showModal(Globals.loading, Globals.timeout, Globals.loadingTimeout);
			getListLectureDownload()

	 //}
	// else
	 //{
	  	 
	 // 	    lbLoading.text=Globals.connecterror;
	 //}

 	return win;
       
    };
})();